package models

type Quality struct {
	BaseModel
	FileList         string `json:"fileList" gorm:"not null;type:LONGTEXT"` //文件
	Name             string `json:"name" gorm:"not null;"`                  //客户姓名
	Phone            string `json:"phone" gorm:"not null;"`                 //联系电话
	CarType          string `json:"carType" gorm:"not null;"`               //车辆型号
	CarNum           string `json:"carNum" gorm:"not null;"`                //车牌号
	CarID            string `json:"carID" gorm:"not null;"`                 //车架号
	Company          string `json:"company" gorm:"not null;"`               //施工单位
	ConstructionTime string `json:"constructionTime" gorm:"not null;"`      //施工时间
	ConstructionPart string `json:"constructionPart" gorm:"not null;"`      //施工部位
	ScrollNum        string `json:"scrollNum" gorm:"not null;"`             //卷轴号码
	QualityTime      string `json:"qualityTime" gorm:"not null;"`           //质保时间
	TypesID          int    `json:"-" gorm:"not null;"`                     //类型
	Types            Types  `json:"types"`
	Price            string `json:"price" gorm:"not null;"` //价格
}

func (u *Quality) CreateTable() {
	err := DB.AutoMigrate(&Quality{})
	if err != nil {
		panic("创建质保表失败:" + err.Error())
	}
}

func (u *Quality) Add() error {
	return DB.Create(u).Error
}

func (c *Quality) FindByPage(query map[string]int) ([]Quality, int64, error) {
	var q []Quality
	var total int64
	DB.Model(&Quality{}).Where(c).Count(&total)
	result := DB.Limit(query["Limit"]).Offset(query["Offset"]).Preload("Types").Find(&q)
	return q, total, result.Error
}

func (c *Quality) FindByPageOr(query map[string]int) ([]Quality, int64, error) {
	var q []Quality
	var total int64
	DB.Model(&Quality{}).Where(c).Count(&total)
	result := DB.Limit(query["Limit"]).Offset(query["Offset"]).Preload("Types").Or(map[string]interface{}{"phone": c.Phone}).Or(map[string]interface{}{"car_num": c.CarNum}).Or(map[string]interface{}{"car_id": c.CarID}).Find(&q)
	return q, total, result.Error
}

func (c *Quality) Update(newDate interface{}) error {
	return DB.Model(&Quality{}).Where(c).Updates(newDate).Error
}

func (c *Quality) Delete() error {
	return DB.Delete(c).Error
}
